package com.lanou.zakerproject.person;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanou.zakerproject.R;
import com.lanou.zakerproject.base.BaseActivity;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;

/**
 * Created by dllo on 16/1/16.
 */
public class PersonActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private ViewPager viewPager;
    //装点点的ImageView数组
    private ImageView[] tips;
    // 装ImageView数组
    private ImageView[] mImageViews;
    //图片id
    private int[] imgIdArray;
    private ImageView imgClose;
    private TextView loginSina, loginQQ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person);
        //mob平台授权
        ShareSDK.initSDK(this);
        ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup);
        viewPager = (ViewPager) findViewById(R.id.login_person);
        imgClose = (ImageView) findViewById(R.id.close_log);
        loginSina = (TextView) findViewById(R.id.login_sina);
        loginQQ = (TextView) findViewById(R.id.login_qq);
        //载入图片资源ID
        imgIdArray = new int[]{R.mipmap.post_login_viewpager_item1, R.mipmap.post_login_viewpager_item2, R.mipmap.post_login_viewpager_item3};
        //将点点加入到ViewGroup中
        tips = new ImageView[imgIdArray.length];
        for (int i = 0; i < tips.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new LayoutParams(10, 10));
            tips[i] = imageView;
            if (i == 0) {
                tips[i].setBackgroundResource(R.drawable.dot_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.dot_normal);
            }

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 5;
            group.addView(imageView, layoutParams);
        }

        //将图片装载到数组中
        mImageViews = new ImageView[imgIdArray.length];
        for (int i = 0; i < mImageViews.length; i++) {
            ImageView imageView = new ImageView(this);
            mImageViews[i] = imageView;
            imageView.setBackgroundResource(imgIdArray[i]);
        }

        //设置Adapter
        viewPager.setAdapter(new MyAdapter());
        //设置监听，主要是设置点点的背景
        viewPager.setOnPageChangeListener(this);
        //设置ViewPager的默认项, 设置为长度的100倍，这样子开始就能往左滑动
        viewPager.setCurrentItem((mImageViews.length) * 100);

        imgClose.setOnClickListener(this);
        loginSina.setOnClickListener(this);
        loginQQ.setOnClickListener(this);


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setImageBackground(position % mImageViews.length);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * 设置选中的tip的背景
     *
     * @param selectItems
     */
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.drawable.dot_focused);
            } else {
                tips[i].setBackgroundResource(R.drawable.dot_normal);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_log:
                finish();
                break;
            case R.id.login_sina:
                //mob第三方登录，要在oncreate初始化
                PlatformActionListener listener2 = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.d("TrainFragment", platform.getDb().getUserId());
                        Log.d("TrainFragment", platform.getDb().getUserName());
                        Log.d("PersonActivity", "hashMap:" + hashMap);
                        Log.d("TrainFragment", "授权成功");
                        finish();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.d("TrainFragment", "授权失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.d("TrainFragment", "取消授权");
                    }
                };
                //登录哪个平台，用哪个名字
                Platform platform2 = ShareSDK.getPlatform(this, SinaWeibo.NAME);
                //判断是否已经授权过，如果授权，删除授权信息重新授权
                if (platform2.isValid()) {
                    platform2.removeAccount();
                }
                platform2.setPlatformActionListener(listener2);
                platform2.showUser(null);
                platform2.authorize();
                break;
            case R.id.login_qq:
                //mob第三方登录，要在oncreate初始化
                PlatformActionListener listener = new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        Log.d("TrainFragment", platform.getDb().getUserId());
                        Log.d("TrainFragment", platform.getDb().getUserName());
                        Log.d("PersonActivity", "hashMap:" + hashMap);
                        Log.d("TrainFragment", "授权成功");
                        finish();
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {
                        Log.d("TrainFragment", "授权失败");
                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.d("TrainFragment", "取消授权");
                    }
                };
                //登录哪个平台，用哪个名字
                Platform platform = ShareSDK.getPlatform(this, QQ.NAME);
                //判断是否已经授权过，如果授权，删除授权信息重新授权
                if (platform.isValid()) {
                    platform.removeAccount();
                }
                platform.setPlatformActionListener(listener);
                platform.showUser(null);
                platform.authorize();
                break;
        }
    }

    public class MyAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        /**
         * 当只有3张图片或者2张图片的时候，滑动存在BUG问题的修改，
         * 注释掉 ((ViewPager) container).removeView(mImageViews[position % mImageViews.length]);
         * 抛出异常
         * destroyItem(View container, int position, Object object)方法中不removeView
         */
        @Override
        public void destroyItem(View container, int position, Object object) {
//            ((ViewPager) container).removeView(mImageViews[position % mImageViews.length]);
        }

        // 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
        @Override
        public Object instantiateItem(View container, int position) {
            //instantiateItem(View container, int position)增加异常扑捉
            try {
                ((ViewPager) container).addView(mImageViews[position % mImageViews.length], 0);
            } catch (Exception e) {

            }
            return mImageViews[position % mImageViews.length];
        }
    }
}

